package concurrent;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock允许未获取到锁时就中断
 * */
public class LockInterrupt {

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("enter");
                    lock.lockInterruptibly();
                    System.out.println("start");
                    TimeUnit.SECONDS.sleep(20);
                    System.out.println("end");
                } catch (InterruptedException e) {
                    System.out.println("InterruptedException。。。");
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }


            }
        });

        // 主线程一直持有锁
        lock.lock();

        thread.start();

        // 启动三秒后中断
        TimeUnit.SECONDS.sleep(3);

        thread.interrupt();
        System.out.println("中断。。。");


    }
}
